//index.js
//获取应用实例
var utils = require('../../utils/util.js');
var app = getApp();
Page({
    data: {
        br:'\n',
        indicatorDots: true,
        autoplay: true,
        interval: 5000,
        duration: 1000,
        bannerloop:true,
        loadingHidden: false, // loading
        swiperCurrent: 0,
        categories: [],
        service_width:0,
        banners:[],//banner 列表
        services:[], // 服务列表
        attitudeArr:[]
    },
    onLoad: function () {
        var that = this;
        wx.setNavigationBarTitle({
            title: app.globalData.shopName
        });
    },
    //解决切换不刷新维内托，每次展示都会调用这个方法
    onShow:function(){
        this.getBanner();
        this.getService();
        this.getAttitude();
        this.getWechatList();

    },
    //事件处理函数
    swiperchange: function (e) {
        this.setData({
            swiperCurrent: e.detail.current
        })
    },
    getBanner: function () {
        var that = this;
        wx.request({
            url: app.buildUrl("/index/rotation"),
            header: app.getRequestHeader(),
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                that.setData({
                    banners: resp.data,
                });
            }
        });
    },
    getService: function () {
        var that = this;
        wx.request({
            url: app.buildUrl("/index/list/7"),
            header: app.getRequestHeader(),
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                that.setData({
                    services: resp.data,
                    service_width:resp.data.length*300+(resp.data.length-1)*20
                });
            }
        });
    },
    getAttitude:function () {
        var that=this;
        wx.request({
            url: app.buildUrl("/index/list/6"),
            header: app.getRequestHeader(),
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                that.setData({
                    attitudeArr: resp.data,
                });
            }
        });
    },
    // 获取微信列表
    getWechatList: function () {
        let that = this;
        wx.request({
            url: app.buildUrl("/wechat/list"),
            header: app.getRequestHeader(),
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                app.globalData.phoneNumber = resp.data[0].mobile
            }
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var that = this;
        return {
            title: that.data.detail.rotationName,
            path: '/pages/welcome/index?openId=' + app.globalData.openId,
            success: function (res) {
                // 转发成功
                wx.request({
                    url: app.buildUrl("/member/share"),
                    header: app.getRequestHeader(),
                    method: 'POST',
                    data: {
                        url: utils.getCurrentPageUrlWithArgs()
                    },
                    success: function (res) {
                        console.log(res)
                    }
                });
            },
            fail: function (res) {
                // 转发失败
            }
        }
    }
});
